package cn.edu.buaa.nlsde.grid.DatabaseConnect;

import java.sql.*;

public class DBConn
{
	
    private Connection conn = null;
    private Statement stat=null;
    private String Driver="org.gjt.mm.mysql.Driver";
    private String url;
   
    public DBConn(String Driver,String url)
    {
    	this.Driver=Driver;
    	this.url=url;
    	try
    	{
    		Class.forName(Driver).newInstance();
    		conn=DriverManager.getConnection(url);
    	}
    	catch(InstantiationException e)
		{
			System.err.println("Instantiation Exception occured!");
			e.printStackTrace();
		}
		catch(IllegalAccessException e)
		{
			System.err.println("IllegalAccess Exception occured!");
			e.printStackTrace();
		}
		catch(ClassNotFoundException e)
		{
			System.err.println("ClassNotFound Exception occured!");
			e.printStackTrace();
		}
		catch(SQLException e)
		{
			System.err.println("SQL Exception occured!");
			e.printStackTrace();
		}
    }
    
    public Statement connect() throws SQLException
    {
    	stat=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    	System.out.println("Succes estblishing the Connection...");
    	return stat;
    }
    
    public void close() throws SQLException
    {
    	stat.close();
    	conn.close();
    	System.out.println("Database has been closed sucessfully!");
    }
}